import axios from 'axios'
import { RequestInterceptor, ResponseInterceptor } from './interceptors'
import { freeEnvConfig, requestConfig } from '@/config'

const baseURL = __INJECT_ENV_VARS__.mode === 'mock' && freeEnvConfig.__MOCK_BASE_URL__ ? freeEnvConfig.__MOCK_BASE_URL__ : import.meta.env.VITE_API_BASE_URL

axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'

const _axios = axios.create({
  timeout: requestConfig.timeout,
  baseURL,
})

const requestInterceptor = new RequestInterceptor()
const responseInterceptor = new ResponseInterceptor()

_axios.interceptors.request.use(
  requestInterceptor.onFulfilled,
  requestInterceptor.onRejected,
  requestInterceptor.options,
)
_axios.interceptors.response.use(
  responseInterceptor.onFulfilled,
  responseInterceptor.onRejected,
)

export default _axios
export * from './methods'
